package com.gk.javaservice.controller;

import com.alibaba.fastjson.JSONArray;
import com.gk.javaservice.common.ServerResponse;
import com.gk.javaservice.po.AdminMenu;
import com.gk.javaservice.po.AdminRoleMenu;
import com.gk.javaservice.service.AdminMenuService;
import com.gk.javaservice.service.AdminRoleMenuService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/AdminMenu")
public class AdminMenuController {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private AdminMenuService adminMenuService;

    @Autowired
    private AdminRoleMenuService adminRoleMenuService;

    //添加
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/add")
    public ServerResponse<?> add(@RequestBody AdminMenu t) {

        log.info(t);


        adminMenuService.add(t);
        AdminRoleMenu ar = new AdminRoleMenu();
        ar.setMenuId((long) t.getId());
        ar.setRoleId((long) 1);
        adminRoleMenuService.add(ar);
        return ServerResponse.createBySuccess(t.getId());

    }

    //根据ID删除1个
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/delete")
    public ServerResponse<?> delete(@RequestBody AdminMenu t) {

        log.info("id:" + t.getId());
        adminMenuService.delete(t.getId());
        return ServerResponse.createBySuccess(t.getId());

    }

    //修改信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/update")
    public ServerResponse<?> update(@RequestBody AdminMenu t) {

        log.info("id:" + t.getId());
        adminMenuService.update(t);
        return ServerResponse.createBySuccess(adminMenuService.hasById(t.getId()));

    }

    //获取所有信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAll")
    public ServerResponse<?> hasAll() {

        log.info("in to hasAll");
        return ServerResponse.createBySuccess(adminMenuService.hasAll());

    }


    //根据ID获取信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasById")
    public ServerResponse<?> hasById(@RequestBody String id) {

        log.info("id:" + id);
        return ServerResponse.createBySuccess(adminMenuService.hasById(id));

    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAllForJSON")
    public ServerResponse<?> hasAllForJSON() {

        log.info("in to hasAllForJSON");
        List<AdminMenu> alist = new ArrayList<AdminMenu>();
        alist = adminMenuService.hasAll();
        JSONArray res = new JSONArray();
        for (AdminMenu aa : alist) {
            if (aa.getParent_id() == 0) {
                aa = setC(aa, alist);
                res.add(aa);
            }

        }
        return ServerResponse.createBySuccess(res);

    }

    public AdminMenu setC(AdminMenu aa, List<AdminMenu> alist) {
        aa.setChildrens(new ArrayList<>());
        for (AdminMenu bb : alist) {
            if (bb.getParent_id() == aa.getId()) {
                bb = setC(bb, alist);
                aa.getChildrens().add(bb);
            }

        }

        return aa;
    }
}
